package com.product.system.controller;

import cn.hutool.core.util.IdUtil;
import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.product.system.domain.ArgpTradesmaType;
import com.product.system.domain.ArgpTradesmanProducts;
import com.product.system.domain.ArgpTradesmanRetailers;
import com.product.system.domain.EcharVo;
import com.product.system.service.ArgpTradesmaTypeService;
import com.product.system.service.ArgpTradesmanProductsService;
import com.product.system.service.ArgpTradesmanRetailersService;
import com.product.system.utils.Result;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@RestController
public class ArgpTradesmanRetailersContrller {

    @Autowired
    private ArgpTradesmanRetailersService argpTradesmanRetailersService;

    @Autowired
    private ArgpTradesmanProductsService argpTradesmanProductsService;

    @Autowired
    private ArgpTradesmaTypeService argpTradesmaTypeService;


    /**
     * 获取所有数据
     * @param current
     * @param size
     * @return
     */
    @GetMapping("getArgpTradesmanRetailers")
    public Result getArgpTradesmanRetailers(Integer current,Integer size,String name){
        PageHelper.startPage(current,size);
        List<ArgpTradesmanRetailers> list = argpTradesmanRetailersService.getArgpTradesmanRetailers(name);
        PageInfo<ArgpTradesmanRetailers> pageInfo = new PageInfo<>(list);
        return Result.success(pageInfo);
    }

    /**
     * 添加数据
     * @return
     */
    @PostMapping("addArgpTradesmanRetailers")
    public Result addArgpTradesmanRestailers(@RequestBody ArgpTradesmanRetailers argpTradesmanRetailers){
        boolean b  = argpTradesmanRetailersService.addArgpTradesmanRestailers(argpTradesmanRetailers);
        return b?Result.success("添加成功"):Result.fail("添加失败");
    }

    /**
     * 获取商品信息
     * @param
     * @return
     */
    @GetMapping("getArgpTradesmanProducts")
    public Result getArgpTradesmanProducts(){
        List<ArgpTradesmanProducts> argpTradesmanProducts = argpTradesmanProductsService.getArgpTradesmanProducts();
        return Result.success(argpTradesmanProducts);
    }


    /**
     * 删除数据
     * @param retailerId
     * @return
     */
    @RequestMapping("/delArgpTradesmanRetailersById")
    public Result delArgpTradesmanRetailersById(Integer retailerId){
        boolean b = argpTradesmanRetailersService.delArgpTradesmanRetailersById(retailerId);
        return b?Result.success():Result.fail();
    }


    /**
     * 修改数据
     * @param retailers
     * @return
     */
    @RequestMapping("/updateArgpTradesmanRetailers")
    public Result updateArgpTradesmanRetailers(@RequestBody ArgpTradesmanRetailers retailers){
        boolean b = argpTradesmanRetailersService.updateArgpTradesmanRetailers(retailers);
        return b?Result.success():Result.fail();
    }


    /**
     * 导出Excel
     * @param response
     * @return
     * @throws IOException
     */
    @GetMapping("/retailersExcel")
    public Result retailersExcel(HttpServletResponse response) throws IOException {
        String fileName = URLEncoder.encode("零售商信息", "UTF-8").replaceAll("\\+", "%20");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-disposition","attachment;filename*=utf-8''"+fileName+".xlsx");
        List<ArgpTradesmanRetailers> list = argpTradesmanRetailersService.selectRetailersExcel();
        EasyExcel.write(response.getOutputStream(), ArgpTradesmanRetailers.class).sheet("零售商信息").doWrite(list);
        return Result.success("导出成功");
    }

    /**
     * 获取echar数据
     * @return
     */
    @RequestMapping("/selectEchar")
    public Result selectEchar(){
        List<EcharVo> list = argpTradesmanRetailersService.selectEchar();
        return Result.success(list);
    }


    /**
     * 查询零售商类型
     * @return
     */
    @RequestMapping("/selectStoreType")
    public Result selectStoreType(){
        List<ArgpTradesmaType> list = argpTradesmaTypeService.selectStoreType();
        return Result.success(list);
    }

//    @RequestMapping("getOCRImage")
//    public String getOCRImage() throws TesseractException {
//        String path = "C:\\Users\\lenovo\\Pictures\\Saved Pictures\\666.jpg";
//        String lagnguagePath = "E:\\OCR\\tessdata";
//        File image = new File(path);
//        System.out.println(image);
//        Tesseract tesseract = new Tesseract();
//        tesseract.setDatapath(lagnguagePath);
//        tesseract.setLanguage("chi_sim");
//        String result = tesseract.doOCR(image);
//        result = result.replace("\n", "").replace(" ", "");
//        System.out.println(result);
//        int startIndex = result.indexOf("名称") + "名称".length();
//        int endIndex = result.indexOf("注册资本");
//        if (startIndex>0 && endIndex>0 && endIndex>startIndex){
//            String name = result.substring(startIndex, endIndex);
//            return name;
//        }else {
//            return "没有找到名称";
//        }
//    }

//    @Autowired
//    private MinioClient minioClient;
//
//    @RequestMapping("uploadRetailers")
//    public String uploadRestailers(MultipartFile file){
//        String fileName = file.getOriginalFilename();
//        fileName = fileName.substring(fileName.indexOf("."));
//        fileName = IdUtil.getSnowflakeNextIdStr()+fileName;
//        try {
//            minioClient.putObject(PutObjectArgs.builder()
//                .bucket("2111a")
//                .contentType(file.getContentType())
//                .stream(file.getInputStream(),file.getSize(),-1)
//                .object(fileName)
//                .build());
//        }catch (Exception e){
//                e.printStackTrace();
//        }
//        String url = "http://127.0.0.1:9000/"+"2111a"+"/"+fileName;
//        return url;
//    }

}
